// 解构并分解值

// 解构结构体
struct Point {
    x: i32,
    y: i32,
}

// 解构枚举类型
enum Message {
    Quite,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}
fn main() {
    // 枚举解构，创建message
    let msg = Message::ChangeColor(0, 160, 255);
    match msg {
        Message::Quite => {
            println!("Quite");
        },
        Message::Move { x, y } => {
            println!("Move X: {}, Y: {}", x, y);
        },
        Message::Write(text) => {
            println!("Write: {}", text);
        },
        Message::ChangeColor(r, g, b) => {
            println!("Change Color: R: {}, G: {}, B: {}", r, g, b);
        },
    }
    // 解构结构体
    let p = Point { x: 1, y: 2 };
    let Point {x: a, y: b} = p;
    let Point { x, y } = p;
    assert_eq!((x,y), (1, 2));
    assert_eq!(a, 1);
    assert_eq!(b, 2);
    // 使用字面值作为结构体模式的一部份进行解构
    let p = Point { x: 1, y: 0 };
    match p {
        Point{x, y: 0} => println!("x: {}", x),
        Point{x: 1, y} => println!("y: {}", y),
        Point{x, y} => println!("x: {}, y: {}::::Other", x, y),
    }
    println!("Hello, world!");
}
